package cn.k.controller;

import cn.k.domain.SystemDictionary;
import cn.k.qo.JSONResult;
import cn.k.qo.QueryObject;
import cn.k.qo.SystemDictionaryQueryObject;
import cn.k.service.ISystemDictionaryService;
import cn.k.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    //处理分页查询页面请求
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") SystemDictionaryQueryObject qo, Model model){
        PageInfo<SystemDictionary> pageInfo = systemDictionaryService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "systemDictionary/list";
    }


    //获取树状结构数据 异步
    @RequestMapping("/treeData")
    @ResponseBody
    public List<Map> treeData(){

        return systemDictionaryService.queryTreeData();
    }



    //处理删除请求
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id){
        if (id!=null){
            systemDictionaryService.delete(id);
            return new JSONResult(true,"删除成功");
        }
        return new JSONResult(false,"删除失败");
    }


    //处理添加或者更新的请求
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(SystemDictionary systemDictionary){
        //判断有没有id
        if (systemDictionary.getId()==null){
            systemDictionaryService.insert(systemDictionary);
        }else {
            systemDictionaryService.update(systemDictionary);
        }
        return new JSONResult(true,"新增/编辑成功");
    }

    @RequestMapping("/queryByParentId")
    @ResponseBody
    public List<SystemDictionary> queryByParentId(Long parentId){
        return systemDictionaryService.queryByParentId(parentId);

    }


}
